package com.springboot_shopping.controller;


import com.springboot_shopping.Mapper.UserMapper;
import com.springboot_shopping.entity.User;
import com.springboot_shopping.service.UserService;
import com.springboot_shopping.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    @GetMapping("/id")
    public ResultVo getUserId(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return ResultVo.error("用户未登录");
        }
        return ResultVo.success("用户ID获取成功", user.getUserId());
    }

    //1、用户注册
    @PostMapping("/register")
    public Map<String, Object> register(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();

        boolean success = userService.register(user);
        if (success) {
            result.put("code", 200);
            result.put("message", "注册成功");
        } else {
            result.put("code", 400);
            result.put("message", "注册失败，手机号已存在或信息不完整");
        }

        return result;
    }

   //2、用户登录
//    @PostMapping("/login")
//    public Map<String, Object> login(@RequestBody Map<String, String> loginData, HttpSession session) {
//        Map<String, Object> result = new HashMap<>();
//
//        String phone = loginData.get("phone");
//        String password = loginData.get("password");
//
//        User user = userService.login(phone, password);
//        if (user != null) {
//            // 登录成功，将用户信息存入Session
//            session.setAttribute("loginUser", user);
//
//            //todo: 返回包含userId的用户信息
//            Map<String, Object> userData = new HashMap<>();
//            userData.put("userId", user.getUserId());
//            userData.put("userName", user.getUserName());
//
//            result.put("code", 200);
//            result.put("message", "登录成功");
//            result.put("data", user);
//        } else {
//            result.put("code", 401);
//            result.put("message", "登录失败，手机号或密码错误");
//        }
//
//        return result;
//    }

    @Transactional
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Map<String, String> loginData, HttpSession session) {
        Map<String, Object> result = new HashMap<>();

        String phone = loginData.get("phone");
        String password = loginData.get("password");

        User user = userService.login(phone, password);
        if (user != null) {
            // 登录成功，将用户信息存入Session
           session.setAttribute("user", user); // 存储整个用户对象
            System.out.println("user,"+user);

            //todo: 返回包含userId的用户信息
//            Map<String, Object> userData = new HashMap<>();
//            userData.put("userId", user.getUserId());
//            userData.put("userName", user.getUserName());
//            userData.put("phone", user.getPhone());

            result.put("code", 200);
            result.put("message", "登录成功");
            result.put("data", user);
        } else {
            result.put("code", 401);
            result.put("message", "登录失败，手机号或密码错误");
        }
        return result;
    }


    //3、获取用户信息
    @GetMapping("/info")
    public User getUserInfo(@RequestParam Integer userId ,HttpSession session, HttpServletRequest request) {
//        Map<String, Object> result = new HashMap<>();
//        String userId = request.getHeader("userId");
        return userMapper.selectById(userId);
//        User loginUser = (User) session.getAttribute("loginUser");
//        if (loginUser != null) {
//            result.put("code", 200);
//            result.put("data", loginUser);
//        } else {
//            result.put("code", 401);
//            result.put("message", "未登录");
//        }
    }
}